package com.ruoyi.recruit.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.domain.*;
import com.ruoyi.recruit.service.IRecruitContentService;
import com.ruoyi.recruit.service.IRecruitInfoService;
import com.ruoyi.recruit.service.IRecruitUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recruit.service.IRecruitUserWrittenService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 笔试人员成绩Controller
 * 
 * @author ruoyi
 * @date 2022-05-05
 */
@Controller
@RequestMapping("/recruit/written")
public class RecruitUserWrittenController extends BaseController
{
    private String prefix = "recruit/written";

    @Autowired
    private IRecruitUserWrittenService recruitUserWrittenService;

    @Autowired
    private IRecruitInfoService recruitInfoService;

    @Autowired
    private IRecruitContentService recruitContentService;

    @Autowired
    private IRecruitUserService recruitUserService;

    @RequiresPermissions("recruit:written:view")
    @GetMapping(value = {"", "/{recruitId}"})
    public String written(@PathVariable(value = "recruitId", required = false) Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(StringUtils.isNotNull(recruitId)){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/written";
    }

    /**
     * 查询笔试人员成绩列表
     */
    @RequiresPermissions("recruit:written:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitUserWritten recruitUserWritten)
    {
        startPage();
        List<RecruitUserWritten> list = recruitUserWrittenService.selectRecruitUserWrittenList(recruitUserWritten);
        return getDataTable(list);
    }

    /**
     * 导出笔试人员成绩列表
     */
    @RequiresPermissions("recruit:written:export")
    @Log(title = "笔试人员成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitUserWritten recruitUserWritten)
    {
        List<RecruitUserWritten> list = recruitUserWrittenService.selectRecruitUserWrittenList(recruitUserWritten);
        ExcelUtil<RecruitUserWritten> util = new ExcelUtil<RecruitUserWritten>(RecruitUserWritten.class);
        return util.exportExcel(list, "笔试人员成绩数据");
    }

    /**
     * 导入笔试人员
     * @param file
     * @param recruitId
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "笔试人员", businessType = BusinessType.IMPORT)
    @RequiresPermissions("recruit:written:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long recruitId, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecruitUserWritten> util = new ExcelUtil<RecruitUserWritten>(RecruitUserWritten.class);
        List<RecruitUserWritten> userWrittenList = util.importExcel(file.getInputStream());
        String message = recruitUserWrittenService.importUserWritten(userWrittenList, recruitId, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("recruit:written:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecruitUserWritten> util = new ExcelUtil<RecruitUserWritten>(RecruitUserWritten.class);
        return util.importTemplateExcel("笔试人员");
    }

    /**
     * 新增笔试人员成绩
     */
    @GetMapping("/add/{recruitId}")
    public String add(@PathVariable("recruitId") Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(recruitId != 0){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/add";
    }

    /**
     * 新增保存笔试人员成绩
     */
    @RequiresPermissions("recruit:written:add")
    @Log(title = "笔试人员成绩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecruitUserWritten recruitUserWritten)
    {
        RecruitContent recruitContent = recruitContentService.checkRecruitContent(recruitUserWritten.getPostName(), recruitUserWritten.getPostCode(), recruitUserWritten.getRecruitId());
        if(StringUtils.isNull(recruitContent)){
            return error("招聘计划不存在");
        }
        recruitUserWritten.setContentId(recruitContent.getContentId());
        RecruitUser recruitUser = recruitUserService.checkRecruitUser(recruitUserWritten.getIdCard(), recruitContent.getContentId());
        if(StringUtils.isNull(recruitUser)){
            return error("应聘人员不存在");
        }
        recruitUserWritten.setUserId(recruitUser.getUserId());
        recruitUserWritten.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserWrittenService.insertRecruitUserWritten(recruitUserWritten));
    }

    /**
     * 修改笔试人员成绩
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RecruitUserWritten recruitUserWritten = recruitUserWrittenService.selectRecruitUserWrittenById(id);
        mmap.put("recruitUserWritten", recruitUserWritten);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/edit";
    }

    /**
     * 修改保存笔试人员成绩
     */
    @RequiresPermissions("recruit:written:edit")
    @Log(title = "笔试人员成绩", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecruitUserWritten recruitUserWritten)
    {
        RecruitContent recruitContent = recruitContentService.checkRecruitContent(recruitUserWritten.getPostName(), recruitUserWritten.getPostCode(), recruitUserWritten.getRecruitId());
        if(StringUtils.isNull(recruitContent)){
            return error("招聘计划不存在");
        }
        recruitUserWritten.setContentId(recruitContent.getContentId());
        RecruitUser recruitUser = recruitUserService.checkRecruitUser(recruitUserWritten.getIdCard(), recruitContent.getContentId());
        if(StringUtils.isNull(recruitUser)){
            return error("应聘人员不存在");
        }
        recruitUserWritten.setUserId(recruitUser.getUserId());
        recruitUserWritten.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recruitUserWrittenService.updateRecruitUserWritten(recruitUserWritten));
    }

    /**
     * 删除笔试人员成绩
     */
    @RequiresPermissions("recruit:written:remove")
    @Log(title = "笔试人员成绩", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitUserWrittenService.deleteRecruitUserWrittenByIds(ids));
    }
}
